package upgrade

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/sagoo-cloud/sagooiot/internal/consts"
	"github.com/sagoo-cloud/sagooiot/internal/dao"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

type sUpgradeDevOtaDetail struct{}

func sUpgradeDevOtaDetailNew() *sUpgradeDevOtaDetail {
	return &sUpgradeDevOtaDetail{}
}
func init() {
	service.RegisterUpgradeDevOtaDetail(sUpgradeDevOtaDetailNew())
}

// GetDevOtaDetailList 获取列表数据
func (s *sUpgradeDevOtaDetail) GetDevOtaDetailList(ctx context.Context, in *model.GetDevOtaDetailListInput) (total, page int, list []*model.DevOtaDetailOutput, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.DevOtaDetail.Ctx(ctx).InnerJoin("dev_device", "dev_device.id = dev_ota_detail.device_id")
		if in.DevOtaStrategy != 0 {
			m = m.Where("dev_ota_detail.strategy_id = ?", in.DevOtaStrategy)
		}
		if in.DeviceKey != "" {
			m = m.Where("dev_device.key like  ?", "%"+in.DeviceKey+"%")
		}
		if in.DeviceName != "" {
			m = m.Where("dev_device.name  like ?", "%"+in.DeviceName+"%")
		}
		if in.Status != 0 {
			m = m.Where("dev_ota_detail.status  = ?", in.Status)
		}

		total, err = m.Count()
		if err != nil {
			err = gerror.New("获取总行数失败")
			return
		}
		page = in.PageNum
		if in.PageSize == 0 {
			in.PageSize = consts.PageSize
		}
		m = m.Fields("dev_device.name device_name,dev_device.key device_key ,dev_ota_detail.*").FieldsEx("dev_device.status")
		err = m.Page(page, in.PageSize).Order("created_at desc").Scan(&list)
		if err != nil {
			err = gerror.New("获取数据失败")
		}
	})
	return
}

// GetDevOtaDetailById 获取指定ID数据
func (s *sUpgradeDevOtaDetail) GetDevOtaDetailById(ctx context.Context, id int) (out *model.DevOtaDetailOutput, err error) {
	err = dao.DevOtaDetail.Ctx(ctx).Where(dao.DevOtaDetail.Columns().Id, id).Scan(&out)
	return
}

// AddDevOtaDetail 添加数据
func (s *sUpgradeDevOtaDetail) AddDevOtaDetail(ctx context.Context, in model.DevOtaDetailAddInput) (err error) {
	_, err = dao.DevOtaDetail.Ctx(ctx).Insert(in)
	return
}

// EditDevOtaDetail 修改数据
func (s *sUpgradeDevOtaDetail) EditDevOtaDetail(ctx context.Context, in model.DevOtaDetailEditInput) (err error) {
	dao.DevOtaDetail.Ctx(ctx).FieldsEx(dao.DevOtaDetail.Columns().Id).Where(dao.DevOtaDetail.Columns().Id, in.Id).OmitEmpty().Update(in)
	return
}

// RecordUpgradeResult 更新升级进度
func (s *sUpgradeDevOtaDetail) RecordUpgradeProcess(ctx context.Context, deviceId, step, strategy int) (err error) {

	data := map[string]interface{}{"step": step, "status": 2}
	if step < 0 {
		data["status"] = 4
		data["fail"] = step
	}

	_, err = dao.DevOtaDetail.Ctx(ctx).Where(dao.DevOtaDetail.Columns().DeviceId, deviceId).
		Where(dao.DevOtaDetail.Columns().StrategyId, strategy).
		Data(data).Update()
	if err != nil {
		g.Log().Errorf(ctx, "RecordUpgradeResult error: %v+", err)
	}

	return
}

func (s *sUpgradeDevOtaDetail) RecordUpgradeResult(ctx context.Context, deviceId, strategy int, version string) (err error) {
	data := map[string]interface{}{"version": version, "status": 2}
	stra, _ := service.UpgradeDevOtaStrategy().GetDevOtaStrategyById(ctx, strategy)
	ware, _ := service.UpgradeDevOtaFireware().GetDevOtaFirewareById(ctx, stra.DevOtaFirewareId)
	if ware.Version == version {
		data["status"] = 3
	}
	_, err = dao.DevOtaDetail.Ctx(ctx).Where(dao.DevOtaDetail.Columns().DeviceId, deviceId).
		Where(dao.DevOtaDetail.Columns().StrategyId, strategy).
		Data(data).Update()
	if err != nil {
		g.Log().Errorf(ctx, "RecordUpgradeResult error: %v+", err)
	}

	return
}

// DeleteDevOtaDetail 删除数据
func (s *sUpgradeDevOtaDetail) DeleteDevOtaDetail(ctx context.Context, Ids []int) (err error) {
	_, err = dao.DevOtaDetail.Ctx(ctx).Delete(dao.DevOtaDetail.Columns().Id+" in (?)", Ids)
	return
}
